//
//  VisitorLoginView.swift
//  sinaOnceAgain
//
//  Created by 王相超 on 16/2/17.
//  Copyright © 2016年 chuanzhi. All rights reserved.
//

import UIKit
import SnapKit

//定义协议
@objc protocol VisitorLoginViewDelegate: NSObjectProtocol {
    
    //声明可选的协议方法  使用optional关键字
    func userWillLogin()
    
    //注册协议的方法
    func userWillRegister()
}

class VisitorLoginView: UIView {
    
    //声明代理  声明代理前一定要使用 weak  防止循环使用
    weak var visitorLoginViewDelegate: VisitorLoginViewDelegate?
    
    
    //自定义访客视图
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    
    //点击事件的监听方法
    @objc private func registerBtnDidClick() {
        
        //使用代理 调用协议方法
        visitorLoginViewDelegate?.userWillRegister()
        
    }
    
    @objc private func loginBtnDidClick() {
        
        //使用代理  调用协议方法
        visitorLoginViewDelegate?.userWillLogin()
        
    }
    
    //外界调用的的方法
    func setupInfoWith(tipText: String, imageName: String?) {
        
        tipLabel.text = tipText
        
        if let img = imageName {
            circleIcon.image = UIImage(named: img)
            largeHouse.hidden = true
            bringSubviewToFront(circleIcon)
            
        }else{
            
            startAnimation()
        }
        
        
    }
    
 
    //设置动画
    private func startAnimation() {
        
        let anima = CABasicAnimation(keyPath: "transform.rotation")
        //重复次数
        anima.repeatCount = MAXFLOAT
        anima.toValue = 2 * M_PI
        //设置动画事件
        anima.duration = 20
        
        //不移除图层
        anima.removedOnCompletion = false
        //将基础动画添加到图层
        circleIcon.layer.addAnimation(anima, forKey: nil)
        
    }
    
    private func setupUI() {
        
        addSubview(circleIcon)
        addSubview(backImage)
        addSubview(largeHouse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        //手写布局
        //屏蔽frame的布局方式
        circleIcon.translatesAutoresizingMaskIntoConstraints = false
        
        //添加约束对象
        circleIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-60)
        }
        
        largeHouse.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
            
        }
        
        backImage.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        
        self.backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleIcon.snp_centerX)
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            //设置宽度
            make.width.equalTo(230)
            //防止页面跳动
            make.height.equalTo(40)
        }
        
        //设置按钮的约束
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(tipLabel.snp_left)
            make.width.equalTo(100)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
        }
        
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(tipLabel.snp_right)
            make.width.equalTo(100)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
        }
        
        
        //添加点击事件
        loginBtn.addTarget(self, action: "loginBtnDidClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //子视图控件   使用懒加载获取
    private lazy var backImage: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    private lazy var circleIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    private lazy var largeHouse: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //提示用户登录的文案
//    private lazy var tipLabel: UILabel = UILabel.factorhLabel("关注一些人,回头看看这里有什么惊喜", fontSize: 14, textColor: UIColor.lightGrayColor())
    
    private lazy var tipLabel: UILabel = UILabel(text: "关注一些人,回头看看这里有什么惊喜", fontSize: 14, textColor: UIColor.lightGrayColor())
    
    //登录按钮
    private lazy var loginBtn: UIButton = UIButton (backImageName: "common_button_white_disable", title: "登录", textColor: UIColor.darkGrayColor())
    
    //注册按钮
    private lazy var registerBtn: UIButton = UIButton(backImageName: "common_button_white_disable", title: "注册", textColor: UIColor.orangeColor())
    
}
